<?php
	/*
	 * Created by Nicky
	 *
	*/
	 /* permission name 
	 kartaskpelajaran_detail
	 kartaskpelajaran_detail_add
	 kartaskpelajaran_detail_edit
	 kartaskpelajaran_detail_del	 
	 */
	class Task_detailController extends CController{
		private $result = array("actionStatus"=>0,"actionMessage"=>"","model"=>array(),"actionError"=>array());	
		public $mainMenu = "Karyawan";
		public $subMenu = "";
		public $title = "";		
		public $loginUser;	
		public function init() {
			parent::init();
			if (!LoginUser::isLogin("karyawan")){
				LoginUser::redirectToLoginPage("karyawan");
			}else{
				$this->loginUser = LoginUser::getUser();
			}
						
		}

 
		public function actionIndex(){
			LoginUser::getPermission("karyawan_taskpelajaran_detail");
			$this->subMenu = "Task Pelajaran Detail";
			$this->layout ="//layouts/ly_blank";
			$karyawan = $this->loginUser->karyawan;
			$karyawan_id=$karyawan->id;	
			$department_id = $karyawan->department_id;	
			$department_name = $karyawan->department->name;					
			$karyawan_name = $karyawan->name;	
 			$module= $this->loginUser ->module;
						
  			if ($module=="akademik")
			{
			$departments = VJadwalPelajaranKelasTahunAjaran::model()->findAll("is_active = TRUE    group by department_id order by id");
			} else 
			{
			$departments = VJadwalPelajaranKelasTahunAjaran::model()->findAll("is_active = TRUE and department_id=:departmentId  group by department_id order by id",array(":departmentId"=>$department_id));
			}

			if ($departments == null){
				$department_id == "";
			}else{	
				$department_id = Yii::app()->request->getParam("department_id", $departments[0]->department_id);
			}
			
	 		$tahun_ajaran_id = Yii::app()->request->getParam("tahun_ajaran_id","");	
	 		$semester_id = Yii::app()->request->getParam("semester_id","");	
	 		$tingkatan_id = Yii::app()->request->getParam("tingkatan_id","");				
	 		$kelas_tahun_ajaran_id = Yii::app()->request->getParam("kelas_tahun_ajaran_id","");			
	 		$pelajaran_id = Yii::app()->request->getParam("pelajaran_id","");		
	 		$walikelas_id = Yii::app()->request->getParam("walikelas_id","");	
			

			$tahunAjarans = VJadwalPelajaranKelasTahunAjaran::model()->findAll("is_active = TRUE and department_id=:departmentId   group by tahun_ajaran_id order by tahun_ajaran_id desc  ",array(":departmentId"=>$department_id));			
			if ($tahunAjarans == null){
				$tahun_ajaran_id == "";
			}else{
				$tahun_ajaran_id = $tahun_ajaran_id == "" ? $tahunAjarans[0]->tahun_ajaran_id : $tahun_ajaran_id;
			}			
			$semesters = MSemester::model()->findAll("tahun_ajaran_id=:tahun_ajaran_id and is_active = TRUE and department_id='".$department_id."' order by id",array(":tahun_ajaran_id"=>$tahun_ajaran_id));
			//$semester_id = "";
			if ($semesters == null){
				$semester_id == "";
			}else{
				$semester_id = $semester_id == "" ? $semesters[0]->id : $semester_id;
			}
            $tingkatans =  VJadwalPelajaranKelasTahunAjaran::model()->findAll("is_active = TRUE and department_id=:departmentId and tahun_ajaran_id=:tahun_ajaran_id  group by tingkatan_id order by id",array(":departmentId"=>$department_id,":tahun_ajaran_id"=>$tahun_ajaran_id));
//            $tingkatan_id = $tingkatans[0]->id ; 
			if ($tingkatans == null){
				$tingkatan_id == "";
			}else{
				$tingkatan_id = $tingkatan_id == "" ? $tingkatans[0]->tingkatan_id : $tingkatan_id;
			}
			
			$mcon= Yii::app()->db;						
			$kelass = VJadwalPelajaranKelasTahunAjaran::model()->findAll("tingkatan_id=:tingkatan_id and department_id=:departmentId and tahun_ajaran_id=:tahun_ajaran_id  and is_active = TRUE group by kelas_id,tingkatan_id order by id",array(":tingkatan_id"=>$tingkatan_id,":departmentId"=>$department_id,":tahun_ajaran_id"=>$tahun_ajaran_id));
 
			if ($kelass == null){
				$kelas_tahun_ajaran_id == "";
			}else{
				$kelas_tahun_ajaran_id = $kelas_tahun_ajaran_id == "" ? $kelass[0]["kelas_tahun_ajaran_id"] : $kelas_tahun_ajaran_id;
			}		
//pelajaran
	 		$pelajarans = Yii::app()->db->createCommand()
				->select('P.id,name,kelas_tahun_ajaran_id')
				->from('m_jadwal_pelajaran JP')
				->join('m_pelajaran P', 'JP.pelajaran_id = P.id')
				->where('kelas_tahun_ajaran_id=:kelas_id and karyawan_id=:karyawan_id', array(':kelas_id'=>$kelas_tahun_ajaran_id,':karyawan_id'=>$karyawan_id))
				->queryAll();
			if ($pelajarans == null){
				$pelajaran_id == "";
			}else{
				$pelajaran_id = $pelajaran_id == "" ? $pelajarans[0]["id"] : $pelajaran_id;
			};
			
							
						
			$data= array(
					"department_id"=>$department_id,
					"departments" => $departments,	
					"karyawan_name"=>$karyawan_name,
					"karyawan_id"=>$karyawan_id,
					"semesters"=>$semesters,
					"semester_id" => $semester_id,
					"tahun_ajaran_id"=>$tahun_ajaran_id,	
					"tahunAjarans"=>$tahunAjarans,	
					"tingkatans"=>$tingkatans,		
					"tingkatan_id"=>$tingkatan_id,	
					"kelass"=>$kelass,
					"kelas_tahun_ajaran_id"=>$kelas_tahun_ajaran_id,
					"pelajarans"=>$pelajarans,
					"pelajaran_id"=>$pelajaran_id,																															
			);
			$this->render('index',$data);							
//			$data=array("department_id"=>"","karyawan_id"=>$karyawan);				
//			$this->render('index',$data);

		}
		
		public function actiondetailList(){		
			$onlyActive = Yii::app()->request->getParam("activeOnly",false);
	 		$tahun_ajaran_id = Yii::app()->request->getParam("tahun_ajaran_id","");	
	 		$semester_id = Yii::app()->request->getParam("semester_id","");	
	 		$tingkatan_id = Yii::app()->request->getParam("tingkatan_id","");				
	 		$kelas_tahun_ajaran_id = Yii::app()->request->getParam("kelas_tahun_ajaran_id","");			
	 		$pelajaran_id = Yii::app()->request->getParam("pelajaran_id","");		
	 		$walikelas_id = Yii::app()->request->getParam("walikelas_id","");	
									
			$mcon= Yii::app()->db;
			$sql1='select TPD.id,TPD.task_date,TPD.task_pelajaran_id,TP.name AS task_name,TPD.name,TPD.notes,TPD.is_active,TP.pelajaran_id, MS.id  as semester_id,KTA.tahun_ajaran_id,tahun_ajaran_name,kelas_name,kelas_id,tingkatan_id,tingkatan_name,KTA.department_id,kelas_tahun_ajaran_id
from m_task_pelajaran_detail TPD 
left join m_task_pelajaran TP on TPD.task_pelajaran_id=TP.id 
left join m_semester MS on (TPD.task_date between start_date and end_date)    
left join v_kelas_tahun_ajaran KTA on KTA.id=TPD.kelas_tahun_ajaran_id    where MS.id=:semester_id and TP.pelajaran_id=:pelajaran_id and TPD.kelas_tahun_ajaran_id =:kelas_tahun_ajaran_id';
			$mTaskPelajaranDetail=$mcon->createCommand($sql1);
			$mTaskPelajaranDetail->bindValue('pelajaran_id',$pelajaran_id);
			$mTaskPelajaranDetail->bindValue('kelas_tahun_ajaran_id',$kelas_tahun_ajaran_id);
			$mTaskPelajaranDetail->bindValue('semester_id',$semester_id);			
			$mTaskPelajaranDetail=$mTaskPelajaranDetail->queryAll();	
			//echo $sql1;
			$this->result["actionStatus"] =0;
			$this->result["actionMessage"] ="";
			$this->result["actionError"] =array();
			$this->result["model"] = $mTaskPelajaranDetail;			
			header('Content-type:application/json');
			echo CJSON::encode($this->result);
		}
		
		public function actionedit(){		
			$op = Yii::app()->request->getParam("op","add");
			$status = Yii::app()->request->getParam("is_active",0) === "on"?1:0 ;			
			if ($op == "add"){
				LoginUser::getPermission("karyawan_taskpelajaran_detail_add");
				//print_r ($_POST);
				//echo "DATA SUDAH DI ADD........";
				$taskpelajarandetail = new MTaskPelajaranDetail;
				$task_date = date_format(date_create_from_format( "d-M-Y", Yii::app()->request->getParam("task_date","01-Jan-2000")),"Y-m-d");					
				$taskpelajarandetail->task_date=$task_date;
				$taskpelajarandetail->task_pelajaran_id = Yii::app()->request->getParam("task_pelajaran_id","");
				$taskpelajarandetail->kelas_tahun_ajaran_id = Yii::app()->request->getParam("kelas_tahun_ajaran_id","");				 
				$taskpelajarandetail->name = Yii::app()->request->getParam("name","");
				$taskpelajarandetail->notes = Yii::app()->request->getParam("notes","-");	
				$taskpelajarandetail->input_date_time=date("Y-m-d H:i:s");								
				$taskpelajarandetail->is_active=$status;
				if ($taskpelajarandetail->save()){
					echo "<script>
						alert('".Yii::t("dialog","Data sudah di tambahkan...!"). "');					
						window.close();					
					</script>";
					exit;
				}else{
					print_r($taskpelajarandetail->getErrors());
				}				
			}else if ($op == "edit"){
				LoginUser::getPermission("karyawan_taskpelajaran_detail_edit");
				//print_r ($_POST);
				$id = Yii::app()->request->getParam("id","");				
				$taskpelajarandetail = MTaskPelajaranDetail::model()->find("id=:id",array(":id"=>$id));
				$task_date = date_format(date_create_from_format( "d-M-Y", Yii::app()->request->getParam("task_date","01-Jan-2000")),"Y-m-d");					
				$taskpelajarandetail->task_date=$task_date;
				$taskpelajarandetail->task_pelajaran_id = Yii::app()->request->getParam("task_pelajaran_id","");
				$taskpelajarandetail->kelas_tahun_ajaran_id = Yii::app()->request->getParam("kelas_tahun_ajaran_id","");				 				
				$taskpelajarandetail->name = Yii::app()->request->getParam("name","");
				$taskpelajarandetail->notes = Yii::app()->request->getParam("notes","-");	
				$taskpelajarandetail->input_date_time=date("Y-m-d H:i:s");						
				$taskpelajarandetail->is_active=$status;
				if ($taskpelajarandetail->save()){
					echo "<script>
						alert('".Yii::t("dialog","Perubahan data sudah di simpan...!"). "');					
						window.close();					
					</script>";
					exit;
				}else{
					print_r($taskpelajarandetail->getErrors());
				}				
			}else if ($op == "del"){
				if (!LoginUser::getPermission("karyawan_taskpelajaran_detail_del",null,false)){
					$this->result["actionStatus"] =1;
					$this->result["actionMessage"] = Yii::t("alert","Anda tidak memiliki ijin untuk menghapus data ini !");
					$this->result["actionError"] =array();
					$this->result["model"] = array();
					header('Content-type:application/json');
					echo CJSON::encode($this->result);
					return;
				}
				$id = Yii::app()->request->getParam("id","");
				$conn = Yii::app()->db;
				$sql = "delete from m_task_pelajaran_detail where id = :id";
				$command = $conn->createCommand($sql);
				try{
					//$command->bindParam(":id",$id,PDO::PARAM_INT);
					if ($command->execute(array(":id"=>$id))){
						$this->result["actionStatus"] =0;
						$this->result["actionMessage"] ="";
						$this->result["actionError"] =array();
						$this->result["model"] = array();
					}
				}catch(Exception $e){
					$this->result["actionStatus"] =2;
					$this->result["actionMessage"] = $e->getMessage();
					$this->result["actionError"] =array();
					$this->result["model"] = array();
				}
				Yii::app()->db->active = false;
			}
			
			header('Content-type:application/json');
			echo CJSON::encode($this->result);
		}
		
		public function actionform(){
		
			$op = Yii::app()->request->getParam("op","add");
			$callback = Yii::app()->request->getParam("callback","");
			$pelajaran_id = Yii::app()->request->getParam("pelajaran_id","0");
			$semester_id = Yii::app()->request->getParam("semester_id","0");
			$task_pelajaran_id = Yii::app()->request->getParam("task_pelajaran_id","0");
			$kelas_tahun_ajaran_id = Yii::app()->request->getParam("kelas_tahun_ajaran_id","");				 			
			$task_date = Yii::app()->request->getParam("task_date", date("d-M-Y"));		
			$tahun_ajaran_id = Yii::app()->request->getParam("tahun_ajaran_id","");						
			$notes = Yii::app()->request->getParam("notes","");									
			
			$data = array("op"=>"add","callback"=>"","id"=>0,"semester_id" =>$semester_id,"kelas_tahun_ajaran_id" =>$kelas_tahun_ajaran_id,"tahun_ajaran_id" =>$tahun_ajaran_id,"task_date" =>$task_date,"pelajaran_id" =>$pelajaran_id,"task_pelajaran_id" =>$task_pelajaran_id,"name"=>"","notes"=>"","is_active"=>"1");
			
			if ($op == "add"){
				LoginUser::getPermission("karyawan_taskpelajaran_detail_add");
				$data["op"] = "add";
				$data["callback"] = $callback;				
			}else if($op =="edit"){				
				LoginUser::getPermission("karyawan_taskpelajaran_detail_edit");			
				$data["op"] = "edit";
				$data["callback"] = $callback;				
				$id = Yii::app()->request->getParam("id","0");				
				$data["id"] = $id;
				
			$mcon= Yii::app()->db;
			$sql1='select task_date, TPD.kelas_tahun_ajaran_id,TPD.task_pelajaran_id,TPD.name, notes,TPD.is_active,pelajaran_id from m_task_pelajaran_detail TPD left join m_task_pelajaran TP on TPD.task_pelajaran_id=TP.id  where TPD.id='.$id;
            $rsTaskPelajaran=$mcon->createCommand($sql1);		
            $rsTaskPelajaran=$rsTaskPelajaran->queryAll();
            $json = CJSON::encode($rsTaskPelajaran); 
            $arrjam = json_decode($json,true);			
							
				if (count($arrjam) > 0){
 
					$task_date = date_format(date_create_from_format( "Y-m-d", $arrjam[0]['task_date']),"d-M-Y");						
					$data["task_date"] = $task_date;	
					$data["task_pelajaran_id"]   =$arrjam[0]["task_pelajaran_id"];									
					$data["pelajaran_id"]   =$arrjam[0]["pelajaran_id"];	
					$data["kelas_tahun_ajaran_id"]   =$arrjam[0]["kelas_tahun_ajaran_id"];									
					$data["name"]   =$arrjam[0]["name"];
					$data["notes"]   =$arrjam[0]["notes"];				
					$data["is_active"]  =$arrjam[0]["is_active"];
				}				
			}			
			$this->layout ="//layouts/ly_blank";
			$this->render("form",$data);
		}
				
		protected function beforeRender($view){
			if ($this->subMenu != ""){
				$this->title = $this->mainMenu . " - " .$this->subMenu;
			}
			return true;
		}
		
	}
?>